﻿@using MongoDB.Bson
@model MyArticle
@{
    if (Model._id == ObjectId.Empty)
    {
        ViewData["Title"] = "新增文章";
    }
    else
    {
        ViewData["Title"] = "编辑文章";
    }
}
<div>
    文章名称：<input type="text" name="articleName" id="articleName" value="" class="col-7"/>
</div>
<hr/>
<textarea name="description" id="description"></textarea>
<hr />
<div>
    <button class="btn-primary col-2" onclick="SaveMyArticle()">保存文章</button>
</div>

@section Scripts
{
    <script src="~/lib/ckeditor/ckeditor.js"></script> 
    <script src="~/js/htmlhelper.js"></script>
    <script type="text/javascript">
        var editer;
        window.onload = function() {
            editer = CKEDITOR.replace('description', { width: 'auto', height: 500, toolbarCanCollapse: true });
            @if (string.IsNullOrEmpty(Model.content))
            {
                @: editer.setData('<p>夜空中最亮的星</p>');
                  @:  sessionStorage.removeItem('acticleid');
                 @: sessionStorage.removeItem('acticledate');
            }
            else
            {
                 @: editer.setData(HtmlUtil.escape2Html('@Html.Raw(Model.content)'));
                 @:  sessionStorage.setItem('acticleid', '@Model._id');
                 @: sessionStorage.setItem('acticledate', '@Model.date');
                 @:  $("#articleName").val('@Html.Raw(Model.title)');
            }

            //editer.on('blur', function (e) {
            //    //console.log(e.editor.data)
            //    //var html = editer.getData().replace(/\n/g,'');
            //    //var strs=HtmlUtil.html2Escape(html)
            //    //console.log(strs);
            //    //SaveMyArticle();
            //});

        };


        //自动保存
        //setInterval("SaveMyArticle()", 60000);

        function SaveMyArticle() {
            var html = editer.getData().replace(/\n/g, '');
            var ids = sessionStorage.getItem('acticleid');
            var dates = sessionStorage.getItem('acticledate');
            var articleName = $("#articleName").val();
            var strs = HtmlUtil.html2Escape(html)
            var formData = new FormData();
            console.log(ids)
            if (ids !== null && typeof (ids) !== "undefined") {
                formData.append('_id',ids);
                formData.append('date', dates);
                formData.append('content', strs);
                formData.append('title', articleName);
                formData.append('status', false);
            } else {
                formData.append('content', strs);
                formData.append('title', articleName);
            }
            $.ajax({
                type: 'POST',
                url: '@Url.Action("SaveHtml")',
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
                dataType: 'json',
                beforeSend: function () {},
                success: function(re) {
                    sessionStorage.setItem('acticleid',re.data._id);
                    sessionStorage.setItem('acticledate', re.data.date);
                },
                complete: function () {},
                error: function (e) {}
            });
        }
    </script>
}

